#include "drv.h"
#include "mid.h"
#include "task.h"
#include "rf_driver_ll_utils.h"
#if USE_CMBACKTRACE == 1
#include <cm_backtrace.h>
//#define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__,(uint32_t) __LINE__))
#define HARDWARE_VERSION               "V1.0.0"
#define SOFTWARE_VERSION               "V0.1.0"
void fault_test_by_unalign(void) {
    volatile int * SCB_CCR = (volatile int *) 0xE000ED14; // SCB->CCR
    volatile int * p;
    volatile int value;

    *SCB_CCR |= (1 << 3); /* bit3: UNALIGN_TRP. */

    p = (int *) 0x00;
    value = *p;
    printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);

    p = (int *) 0x04;
    value = *p;
    printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);

    p = (int *) 0x03;
    value = *p;
    printf("addr:0x%02X value:0x%08X\r\n", (int) p, value);
}
void PrintNegotiatedLevel(uint8_t stopLevel)
{
  printf("Power save level negotiated: ");
  switch (stopLevel)
  { 
  case POWER_SAVE_LEVEL_RUNNING:
    printf ("RUNNING\r\n");
    break;
  case POWER_SAVE_LEVEL_CPU_HALT:
    printf ("CPU_HALT\r\n");
    break;
  case POWER_SAVE_LEVEL_STOP_WITH_TIMER:
    printf ("STOP_WITH_TIMER\r\n");
    break;
  case POWER_SAVE_LEVEL_STOP_NOTIMER:
    printf ("STOP_NOTIMER\r\n");
    break;
  }
}


#endif
int main(void) 
{
	Drv_Init() ;
#if USE_CMBACKTRACE == 1
	cm_backtrace_init("cgm", HARDWARE_VERSION, SOFTWARE_VERSION);
#endif
	Mid_Init() ; 
	Task_Init() ;
	BSP_PB_Init(BSP_PUSH1, BUTTON_MODE_EXTI);
	while(1)
	{
		Task_Process() ;
	}

}

/* Event used to notify the Host that a hardware failure has occurred in the Controller. 
See bluenrg_lp_events.h.
 *        - 0x00: Generic HW error
 *        - 0x01: Radio state error
 *        - 0x02: Timer overrun error
 *        - 0x03: Internal queue overflow error
 *        - 0x04: Late Radio ISR
 *        - 0x05: TX Error */
void hci_hardware_error_event(uint8_t Hardware_Code)
{
	if (Hardware_Code <= 0x03)
	{
		LOG_INFO("Hardware_Code =%d\n",Hardware_Code);
		NVIC_SystemReset();
	}
}


/**
* This event is generated to report firmware error informations.
* FW_Error_Type possible values: 
* Values:
- 0x01: L2CAP recombination failure
- 0x02: GATT unexpected response
- 0x03: GATT unexpected request
After this event with error type (0x01, 0x02, 0x3) it is recommended to disconnect. 
*/
void aci_hal_fw_error_event(uint8_t FW_Error_Type,
			    uint8_t Data_Length,
			    uint8_t Data[])
{
	if (FW_Error_Type <= 0x03)
	{
		uint16_t connHandle;
		
		/* Data field is the connection handle where error has occurred */
		connHandle = LE_TO_HOST_16(Data);
		
		aci_gap_terminate(connHandle, BLE_ERROR_TERMINATED_REMOTE_USER); 
	}
}

/***************************************************************************************/

#ifdef USE_FULL_ASSERT
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(uint8_t* file, uint32_t line)
{
	/* User can add his own implementation to report the file name and line number,
	ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
	
	/* Infinite loop */
	while (1)
	{}
}
#endif

